<template>
  <div class="hacker-os">
    <!-- 系统状态栏 -->
    <div class="system-status-bar">
      <div class="status-left">
        <span class="status-icon">●</span>
        <span class="status-text">系统: NEXUS操作系统</span>
      </div>
      <div class="status-center">
        <span class="status-item">连接数: {{ connections }}</span>
        <span class="status-item">用户: {{ counter.userInfo.userName }}</span>
        <span class="status-item">安全等级: {{ securityLevel === 'HIGH' ? '高级' : securityLevel }}</span>
      </div>
      <div class="status-right">
        <span class="status-time">{{ currentTime }}</span>
      </div>
    </div>
    
    <!-- 主容器 -->
    <el-container class="main-container">
      <el-header class="terminal-header">
        <!-- 一行两列 -->
        <el-row>
          <el-col :span="12" class="header-left">
            <el-icon><Grid class="hacker-icon" /></el-icon>
            <span class="header-title">NEXUS权限管理系统</span>
          </el-col>
          <el-col :span="12" class="header-right">
            <el-icon><Avatar class="hacker-icon" /></el-icon>
            <span class="user-info">
              {{ counter.userInfo.userName }} , {{ counter.userInfo.roleName }} , {{ counter.userInfo.userNickname }}
            </span>
            <el-icon class="logout-icon" @click="quit">
              <SwitchButton class="hacker-icon" />
            </el-icon>
          </el-col>
        </el-row>
      </el-header>
      
      <el-container>
        <el-aside width="240px" class="menu-container">
          <!-- 使用高级菜单组件 -->
          <AdvancedMenu />
        </el-aside>
        
        <el-main class="main-content">
          <!-- 内容区域装饰线 -->
          <div class="content-decoration"></div>
          <!-- 路由视图 -->
          <RouterView></RouterView>
        </el-main>
      </el-container>
    </el-container>
    
    <!-- 数字雨背景效果 -->
    <div class="matrix-rain"></div>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref, onMounted, onUnmounted } from 'vue'
import { useRouter, RouterView, useRoute } from 'vue-router'
import { ElMessage, type FormInstance, type FormRules, ElMessageBox } from 'element-plus'
import axios from 'axios'
const router = useRouter()
const route = useRoute()
import {
  Grid,
  Avatar,
  SwitchButton
} from '@element-plus/icons-vue'

// 引入高级菜单组件
import AdvancedMenu from '../components/AdvancedMenu.vue'

// 引入pinia
import { useCounterStore } from "@/stores/counter"
// pinia变量
const counter = useCounterStore()

// 系统状态
const currentTime = ref('')
const connections = ref('001')
const securityLevel = ref('HIGH')
let timeInterval: number

// 退出登录
const quit = () => {
  // 友好提示 - 更新为黑客风格对话框
  ElMessageBox.confirm(
    '系统: 确认退出登录流程?',
    '安全警告',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
    .then(() => {
      // 点击确定
      // 模拟系统清理过程
      systemLog('SYSTEM: INITIATING LOGOUT SEQUENCE')
      
      setTimeout(() => {
        systemLog('SYSTEM: CLEARING USER SESSION DATA')
      }, 300)
      
      setTimeout(() => {
        systemLog('SYSTEM: RESETTING PERMISSION LEVELS')
      }, 600)
      
      setTimeout(() => {
        systemLog('SYSTEM: LOGOUT COMPLETE')
        // 清除信息
        counter.userInfo.userId = 0;
        counter.userInfo.userName = "";
        counter.userInfo.userNickname = "";
        counter.userInfo.roleName = "";
        counter.userInfo.roleId = 0;
        // 跳转至登录
        router.push('/')
      }, 900)
    })
    .catch(() => {
      systemLog('SYSTEM: LOGOUT ABORTED BY USER')
      ElMessage({
        type: 'info',
        message: 'SYSTEM: LOGOUT ABORTED',
      })
    })
}

// 系统日志函数
const systemLog = (message: string) => {
  console.log(`[${new Date().toLocaleTimeString()}] ${message}`)
}

// 模拟系统连接数变化
const simulateConnections = () => {
  // 随机增减连接数，保持在合理范围内
  const currentConn = parseInt(connections.value)
  const change = Math.floor(Math.random() * 3) - 1 // -1, 0, 1
  const newConn = Math.max(1, Math.min(999, currentConn + change))
  connections.value = newConn.toString().padStart(3, '0')
}

// 更新当前时间
const updateTime = () => {
  const now = new Date()
  currentTime.value = now.toLocaleTimeString('en-US', {
    hour12: false,
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit'
  })
}

// 初始化数字雨效果
const initMatrixRain = () => {
  const container = document.querySelector('.hacker-os')
  if (!container) return
  
  const matrixRain = document.querySelector('.matrix-rain')
  if (!matrixRain) return
  
  // 清除已有字符
  matrixRain.innerHTML = ''
  
  const chars = '10101010101010101010'
  
  // 创建100个随机字符
  for (let i = 0; i < 100; i++) {
    const charElement = document.createElement('div')
    charElement.classList.add('matrix-char')
    charElement.textContent = chars[Math.floor(Math.random() * chars.length)]
    charElement.style.left = `${Math.random() * 100}%`
    charElement.style.top = `${Math.random() * 100}%`
    charElement.style.fontSize = `${Math.random() * 8 + 4}px`
    charElement.style.animationDuration = `${Math.random() * 10 + 10}s`
    charElement.style.animationDelay = `${Math.random() * 5}s`
    matrixRain.appendChild(charElement)
  }
}

// 组件挂载时初始化
onMounted(() => {
  systemLog('SYSTEM: MAIN MODULE INITIALIZED')
  systemLog('SYSTEM: RBAC MANAGEMENT SYSTEM ONLINE')
  
  // 启动时间更新
  updateTime()
  timeInterval = window.setInterval(() => {
    updateTime()
    // 每10秒更新一次连接数
    if (Math.random() > 0.9) {
      simulateConnections()
    }
  }, 1000)
  
  // 初始化数字雨效果
  initMatrixRain()
})

// 组件卸载时清理
onUnmounted(() => {
  if (timeInterval) {
    clearInterval(timeInterval)
  }
  systemLog('SYSTEM: MAIN MODULE SHUTTING DOWN')
})
</script>

<style scoped>
/* 黑客操作系统容器 */
.hacker-os {
  width: 100%;
  height: 100vh;
  background-color: var(--hacker-dark);
  color: var(--hacker-text);
  font-family: 'Courier New', monospace;
  position: relative;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  /* 黑客风格网格背景 */
  background-image: 
    linear-gradient(rgba(0, 255, 65, 0.03) 1px, transparent 1px),
    linear-gradient(90deg, rgba(0, 255, 65, 0.03) 1px, transparent 1px);
  background-size: 20px 20px;
}

/* 系统状态栏 */
.system-status-bar {
  height: 18px;
  background-color: var(--hacker-panel-dark);
  border-bottom: 1px solid var(--hacker-border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 10px;
  font-size: 11px;
  color: var(--hacker-green);
  z-index: 200;
}

.status-left {
  display: flex;
  align-items: center;
  gap: 5px;
}

.status-icon {
  color: var(--hacker-green);
  animation: statusPulse 2s infinite alternate;
}

.status-center {
  display: flex;
  gap: 15px;
}

.status-item {
  color: var(--hacker-text);
}

.status-right {
  color: var(--hacker-text);
}

/* 主容器 */
.main-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  background-color: transparent;
}

/* 终端风格头部 */
.terminal-header {
  height: 45px;
  background-color: var(--hacker-panel);
  border-bottom: 1px solid var(--hacker-border);
  display: flex;
  align-items: center;
  box-shadow: 0 2px 10px rgba(0, 255, 65, 0.1);
}

.header-left {
  display: flex;
  align-items: center;
  gap: 8px;
}

.header-title {
  color: var(--hacker-green);
  font-size: 14px;
  letter-spacing: 1px;
  text-transform: uppercase;
  font-weight: bold;
  text-shadow: 0 0 5px var(--hacker-green-glow);
}

.header-right {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 8px;
}

.user-info {
  color: var(--hacker-text);
  font-size: 12px;
}

.logout-icon {
  cursor: pointer;
  padding: 4px;
  border-radius: 3px;
  transition: all 0.3s ease;
}

.logout-icon:hover {
  background-color: rgba(0, 255, 65, 0.1);
  box-shadow: 0 0 10px rgba(0, 255, 65, 0.3);
}

/* 黑客风格图标 */
.hacker-icon {
  color: var(--hacker-green);
  transition: all 0.3s ease;
}

.hacker-icon:hover {
  filter: drop-shadow(0 0 5px var(--hacker-green-glow));
}

/* 菜单容器 */
.menu-container {
  height: calc(100vh - 63px); /* 100vh - 状态栏高度 - 头部高度 */
  background-color: var(--hacker-panel-dark);
  border-right: 1px solid var(--hacker-border);
  transition: all 0.3s ease;
  overflow-y: auto;
}

/* 当菜单折叠时调整布局 */
.menu-collapsed .menu-container {
  width: 64px !important;
}

.menu-collapsed .main-content {
  margin-left: 64px !important;
}

/* 主内容区域 */
.main-content {
  padding: 20px;
  overflow-y: auto;
  background-color: transparent;
  position: relative;
  z-index: 10;
}

/* 内容装饰线 */
.content-decoration {
  height: 1px;
  background: linear-gradient(90deg, transparent, var(--hacker-green), transparent);
  margin-bottom: 20px;
  box-shadow: 0 0 10px var(--hacker-green-glow);
}

/* 数字雨背景效果 */
.matrix-rain {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 1;
}

.matrix-char {
  position: absolute;
  color: rgba(0, 255, 65, 0.08);
  font-family: 'Courier New', monospace;
  opacity: 0;
  animation: matrixRain linear infinite;
  transform-origin: 0 0;
}

/* 动画效果 */
@keyframes matrixRain {
  0% {
    transform: translateY(-10%) rotate(0deg);
    opacity: 0;
  }
  10% {
    opacity: 0.5;
  }
  90% {
    opacity: 0.5;
  }
  100% {
    transform: translateY(110%) rotate(10deg);
    opacity: 0;
  }
}

@keyframes statusPulse {
  0% {
    color: var(--hacker-green);
    text-shadow: 0 0 2px var(--hacker-green-glow);
  }
  100% {
    color: var(--hacker-green);
    text-shadow: 0 0 8px var(--hacker-green-glow);
  }
}

/* 自定义滚动条 */
::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}

::-webkit-scrollbar-track {
  background: var(--hacker-panel-dark);
}

::-webkit-scrollbar-thumb {
  background: var(--hacker-green-dark);
  border: 1px solid var(--hacker-green-glow);
}

::-webkit-scrollbar-thumb:hover {
  background: var(--hacker-green);
  box-shadow: 0 0 8px var(--hacker-green-glow);
}

/* 响应式设计 */
@media (max-width: 1024px) {
  .menu-container {
    width: 64px !important;
  }
  
  .main-content {
    padding: 15px;
  }
  
  .header-title {
    font-size: 12px;
    letter-spacing: 0.5px;
  }
  
  .user-info {
    font-size: 11px;
  }
  
  .system-status-bar {
    font-size: 10px;
  }
}

@media (max-width: 768px) {
  .terminal-header {
    padding: 0 10px;
  }
  
  .header-left,
  .header-right {
    flex-direction: column;
    gap: 4px;
  }
  
  .main-content {
    padding: 10px;
  }
  
  .header-title {
    display: none;
  }
  
  .user-info {
    display: none;
  }
  
  .system-status-bar {
    height: 16px;
  }
  
  .status-center {
    gap: 8px;
  }
  
  .status-item {
    font-size: 9px;
  }
}
</style>
